function refreshAllActivities(alias) {
    if (alias && alias.indexOf("-") > 0) refreshIssueActivities(alias);
    else if (alias) refreshProjectActivities(alias);
    updateDashboardGrids();
}

function refreshIssueActivities(alias) {
    var issueAlias = alias.replace("-", "_");
    var gridIssueActivity = dijit.byId("grid" + issueAlias + "IssueActivity");
    if (gridIssueActivity) {
        gridIssueActivity.store.initial = true;
        entityFilter(issueAlias + "IssueActivity", "Activity");
    }
    refreshProjectActivities(alias.substring(0, alias.lastIndexOf("-")));
}

function refreshProjectActivities(alias) {
    var gridProjectActivity = dijit.byId("grid" + alias + "ProjectActivity");
    if (gridProjectActivity) {
        gridProjectActivity.store.initial = true;
        entityFilter(alias + "ProjectActivity", "Activity");
    }
}

function updateDashboardGrids() {
    var gridMyProjects = dijit.byId("gridmyProjects");
    if (gridMyProjects) {
        gridMyProjects.store.initial = true;
        entityFilter('myProjects', 'DefaultProject');
    }
    var gridAllProjects = dijit.byId("gridallProjects");
    if (gridAllProjects) {
        gridAllProjects.store.initial = true;
        entityFilter('allProjects', 'DefaultProject');
    }
    var gridMyActivities = dijit.byId("gridmyActivities");
    if (gridMyActivities) {
        gridMyActivities.store.initial = true;
        entityFilter('myActivities', 'Activity');
    }
    var gridAllActivities = dijit.byId("gridactivities");
    if (gridAllActivities) {
        gridMyActivities.store.initial = true;
        entityFilter('activities', 'Activity');
    }
    fireStoreFetchQueue();
}

function dashboardOnLoad() {
    var grid = dijit.byId("gridallProjects");
    if (grid) {
        showprojectActivityFilter("all", true, true);
        var joinIcon = byId("joinProjectIcon");
        if (joinIcon) dojo.style(joinIcon, "opacity", "0.4");
        dojo.connect(grid, "onRowClick", function (e) {
            var opacity = (grid.selection.selectedIndex >= 0) && (grid.selection.getSelected()[0] != null) ? "1" : "0.4";
            if (joinIcon) dojo.style(joinIcon, "opacity", opacity);
            dojo.style(byId("spriteopenProject"), "opacity", opacity);
        });
        dojo.connect(grid, "onRowDblClick", function (e) {
            openProjectTab(grid.selection);
        });
    } else {
        setTimeout(dashboardOnLoad, 500);
    }
}

function adminOnLoad() {
    var grid = dijit.byId("gridadminPollGrid");
    if (grid) {
        dojo.connect(grid, "onRowClick", function (e) {
            var opacity = (grid.selection.selectedIndex >= 0) && (grid.selection.getSelected()[0] != null) ? "1" : "0.4";
            dojo.style(byId("spriteadminPollEdit"), "opacity", opacity);
            dojo.style(byId("spriteadminPollToggle"), "opacity", opacity);
        });
    } else {
        setTimeout(adminOnLoad, 400);
    }
}

function myProjectsOnLoad() {
    var grid = dijit.byId("gridmyProjects");
    if (grid) {
        showprojectActivityFilter("my", false, true);
        dojo.connect(grid, "onRowClick", function (e) {
            var opacity = (grid.selection.selectedIndex >= 0) && (grid.selection.getSelected()[0] != null) ? "1" : "0.4";
            dojo.style(byId("spriteopenMyProject"), "opacity", opacity);
        });
        dojo.connect(grid, "onRowDblClick", function (e) {
            openProjectTab(grid.selection);
        });
    } else {
        setTimeout(myProjectsOnLoad, 500);
    }
}

function issueOnLoad(id) {
    if (dijit.byId("filterSearchTabContainer" + id)) resizeFilter(id);
    else {
        setTimeout(function() {
            issueOnLoad(id);
        }, 250);
    }
}

function openProjectTab(selection) {
    if (selection && selection.selectedIndex >= 0) {
        var selected = selection.getSelected();
        if (selected && selected[0]) {
            var project = selected[0];
            var id = dojo.isArray(project.id) ? project.id[0] : project.id;
            var name = dojo.isArray(project.displayable) ? project.displayable[0] : project.displayable;
            openOrSelectTab(id, name, "project");
        }
    }
}

function openOrSelectTab(id, name, which) {
    var tc = dijit.byId("mainTabContainer");
    var child = isTabOpen(tc, name);
    if (!child) {
        var cp = new dojox.layout.ContentPane({
            title: name,
            closable: true,
            href: contextPath + which + ".iwebmvc?" + which + "=" + id + "&cacheBust=" + Math.random(),
            onLoad: fireStoreFetchQueue
        });
        tc.addChild(cp);
        tc.selectChild(cp);
    } else {
        tc.selectChild(child);
    }
}

function closeTab(container, title) {
    if (container) {
        var child = isTabOpen(container, title);
        if (child) container.removeChild(child);
    }
}

function isTabOpen(container, title) {
    if (container && title) {
        var tabs = container.getChildren();
        for (var index = 0; index < tabs.length; index++) {
            if (tabs[index].title == title) {
                return tabs[index];
            }
        }
    }
    return null;
}

function showIssuesFilter(alias, top) {
    if (top) {
        dojo.style(byId("sprite" + alias + "IssuesFilter"), "display", "none");
        dojo.style(byId("sprite" + alias + "IssuesHide"), "display", "inline");
        dojo.style(byId("filterGroup" + alias + "Issues"), "display", "block");
        resizeFilter(alias + "Issues");
    } else {
        dojo.style(byId("sprite" + alias + "ProjectActivityFilter"), "display", "none");
        dojo.style(byId("sprite" + alias + "ProjectActivityHide"), "display", "inline");
        dojo.style(byId("filterGroup" + alias + "ProjectActivity"), "display", "block");
        resizeFilter(alias + "ProjectActivity");
    }
    hideIssuesFilter(alias, !top);
}

function hideIssuesFilter(alias, top) {
    if (top) {
        dojo.style(byId("sprite" + alias + "IssuesFilter"), "display", "inline");
        dojo.style(byId("sprite" + alias + "IssuesHide"), "display", "none");
        dojo.style(byId("filterGroup" + alias + "Issues"), "display", "none");
    } else {
        dojo.style(byId("sprite" + alias + "ProjectActivityFilter"), "display", "inline");
        dojo.style(byId("sprite" + alias + "ProjectActivityHide"), "display", "none");
        dojo.style(byId("filterGroup" + alias + "ProjectActivity"), "display", "none");
    }
}

function projectOnLoad(alias) {
    var grid = dijit.byId("grid" + alias + "Issues");
    if (grid) {
        resizeFilter(alias + "Issues");
        dojo.connect(grid, "onRowClick", function (e) {
            var opacity = (grid.selection.selectedIndex >= 0) && (grid.selection.getSelected()[0] != null) ? "1" : "0.4";
            dojo.style(byId("sprite" + alias + "OpenIssue"), "opacity", opacity);
        });
        dojo.connect(grid, "onRowDblClick", function (e) {
            openIssueTab(grid.selection);
        });
    } else {
        setTimeout(function() {
            projectOnLoad(alias);
        }, 500);
    }
}

function openIssueTab(selection) {
    if (selection && selection.selectedIndex >= 0) {
        var selected = selection.getSelected();
        if (selected && selected[0]) {
            var issue = selected[0];
            var id = dojo.isArray(issue.id) ? issue.id[0] : issue.id;
            var name = dojo.isArray(issue.displayable) ? issue.displayable[0] : issue.displayable;
            openOrSelectTab(id, name, "issue");
        }
    }
}

function showprojectActivityFilter(which, project, show) {
    dojo.style(byId("sprite" + which + "ProjectsFilter"), "display", project && show ? "none" : "inline");
    dojo.style(byId("sprite" + which + "ProjectsHide"), "display", project && show ? 'inline' : "none");
    dojo.style(byId("filterGroup" + which + "Projects"), "display", project && show ? "block" : "none");
    dojo.style(byId("sprite" + (which == "all" ? "a" : "myA") + "ctivitiesFilter"), "display", !project && show ? "none" : "inline");
    dojo.style(byId("sprite" + (which == "all" ? "a" : "myA") + "ctivitiesHide"), "display", !project && show ? "inline" : "none");
    dojo.style(byId("filterGroup" + (which == "all" ? "a" : "myA") + "ctivities"), "display", !project && show ? "block" : "none");
    var filter = project && show ? which + "Projects" : (which == "all" ? "a" : "myA") + "ctivities";
    resizeFilter(filter);
}

function showIssueActivityFilter(alias) {
    dojo.style(byId("sprite" + alias + "IssueActivityFilter"), "display", "none");
    dojo.style(byId("sprite" + alias + "IssueActivityHide"), "display", "inline");
    dojo.style(byId("filterGroup" + alias + "IssueActivity"), "display", "block");
    resizeFilter(alias + "IssueActivity");
    dojo.query("#filterGroup" + alias + "IssueActivity .iconActionContainer").forEach(function(item, index, array){
        if (index > 0) dojo.style(item, "opacity", "0.5");
    });
}

function hideIssueActivityFilter(alias) {
    dojo.style(byId("sprite" + alias + "IssueActivityFilter"), "display", "inline");
    dojo.style(byId("sprite" + alias + "IssueActivityHide"), "display", "none");
    dojo.style(byId("filterGroup" + alias + "IssueActivity"), "display", "none");
}